package sim1011.examples.kame.test;

import java.util.Random;

import jade.core.AID;
import jade.core.ContainerID;
import jade.core.behaviours.CyclicBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;

public class ComponentBehaviour extends CyclicBehaviour {
	private static final long serialVersionUID = -4892058773573619829L;
	private AID[] components;
	private AID[] brain;
	
	@Override
	public void action() {
		ACLMessage msg = myAgent.receive();
		if (msg != null) {
			if (msg.getPerformative() == ACLMessage.INFORM) {
				String content = msg.getContent();
				System.out.println(content);
				moveTo(content);
				execute();
			} else if (msg.getPerformative() == ACLMessage.PROPAGATE) {
				DFAgentDescription template = new DFAgentDescription();
				ServiceDescription sd = new ServiceDescription();
				sd.setType("sim-type");
				sd.setName("sim-small-brain");
				template.addServices(sd);
				try {
					DFAgentDescription[] result = DFService.search(myAgent, template); 
					brain = new AID[result.length];
					for (int i = 0; i < result.length; ++i) {
						brain[i] = result[i].getName();
					}
					if (result.length != 0) {
						ACLMessage req = new ACLMessage(ACLMessage.REQUEST);
						req.setContent("Request where to execute");
						System.out.println("sending request");
						req.addReceiver(brain[0]);
						myAgent.send(req);
					}
				} catch (FIPAException fe) {
					fe.printStackTrace();
				}
			}
		} else {
			block();
		}
	}
	
	private void moveTo(String content) {
		ContainerID id = new ContainerID();
		id.setName(content);
		System.out.println("moving to: " + content);
		//Object[] args = myAgent.getArguments();
		//Object[] newArgs = {(SmallBrainAgent)args[0], ((SmallBrainAgent)args[0]).getCurrentProperties(content)};
		//myAgent.setArguments(newArgs);
		myAgent.doMove(id);
	}

	private void execute() {
		try {
			Object[] prop = myAgent.getArguments();
			ContainerProperties p = (ContainerProperties)prop[1];
			if (p.isMobile())
				p = (MobileContainerProperties)p;
			p.setCPU(p.getCPU() - 20);
			p.setMEM(p.getMEM() - 10);
			Random r = new Random(System.currentTimeMillis());
			Thread.sleep(r.nextInt(10000)+1);
			sendMessage();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	private void sendMessage() {
		DFAgentDescription template = new DFAgentDescription();
		ServiceDescription sd = new ServiceDescription();
		sd.setType("sim-type");
		template.addServices(sd);
		try {
			DFAgentDescription[] result = DFService.search(myAgent, template); 
			components = new AID[result.length];
			for (int i = 0; i < result.length; ++i) {
				components[i] = result[i].getName();
			}
			if (result.length != 0) {
				Random r = new Random(System.currentTimeMillis());
				int nr = -1;
				boolean found = false;
				int who = -1;
				while (!found) {
					nr = r.nextInt();
					who = (Math.abs(nr))%(result.length);
					if (!components[who].getName().equalsIgnoreCase(myAgent.getAID().getName()))
						found = true;
				}
				ACLMessage req = new ACLMessage(ACLMessage.PROPAGATE);
				req.addReceiver(components[who]);
				myAgent.send(req);
			}
		} catch (FIPAException fe) {
			fe.printStackTrace();
		}
	}
}
